<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="采集时间" prop="collectTime">
        <el-input
          v-model="queryParams.collectTime"
          placeholder="请输入采集时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="采集地点" prop="collectAddr">
        <el-input
          v-model="queryParams.collectAddr"
          placeholder="请输入采集地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物候期" prop="cropPp">
        <el-input
          v-model="queryParams.cropPp"
          placeholder="请输入物候期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长势分级" prop="cropGg">
        <el-input
          v-model="queryParams.cropGg"
          placeholder="请输入长势分级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="病虫害" prop="cropPd">
        <el-input
          v-model="queryParams.cropPd"
          placeholder="请输入病虫害"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否缺乏某种营养" prop="sfqfyy">
        <el-input
          v-model="queryParams.sfqfyy"
          placeholder="请输入是否缺乏某种营养"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="是否启用" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入是否启用(1：启用，2：禁用）"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:crop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:crop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:crop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:crop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cropList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="区域ID" align="center" prop="cropId" />
      <el-table-column label="采集时间" align="center" prop="collectTime" />
      <el-table-column label="采集地点" align="center" prop="collectAddr" />
      <el-table-column label="作物品种" align="center" prop="cropType" />
      <el-table-column label="物候期" align="center" prop="cropPp" />
      <el-table-column label="长势分级" align="center" prop="cropGg" />
      <el-table-column label="病虫害" align="center" prop="cropPd" />
      <el-table-column label="是否缺乏某种营养" align="center" prop="sfqfyy" />
      <!-- <el-table-column label="是否启用(1：启用，2：禁用）" align="center" prop="state" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:crop:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:crop:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改作物信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="采集时间" prop="collectTime">
          <!-- <el-input v-model="form.collectTime" placeholder="请输入采集时间" /> -->
          <el-date-picker
            v-model="form.collectTime"
            type="date"
            placeholder="选择采集时间"
            value-format="yyyy-MM-dd"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="采集地点" prop="collectAddr">
          <el-input v-model="form.collectAddr" placeholder="请输入采集地点" />
        </el-form-item>
        <el-form-item label="株高" prop="cropHeight">
          <el-input v-model="form.cropHeight" placeholder="请输入株高" />
        </el-form-item>
        <el-form-item label="物候期" prop="cropPp">
          <el-input v-model="form.cropPp" placeholder="请输入物候期" />
        </el-form-item>
        <el-form-item label="长势分级" prop="cropGg">
          <el-input v-model="form.cropGg" placeholder="请输入长势分级" />
        </el-form-item>
        <el-form-item label="病虫害" prop="cropPd">
          <el-input v-model="form.cropPd" placeholder="请输入病虫害" />
        </el-form-item>
        <el-form-item label="是否缺乏某种营养" prop="sfqfyy">
          <!-- <el-input v-model="form.sfqfyy" placeholder="请输入是否缺乏某种营养" /> -->
          <el-select v-model="form.sfqfyy" placeholder="请选择是否缺乏某种营养" value-key="value">
            <el-option key="1" label="是" value="1"></el-option>
            <el-option key="2" label="否" value="2"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否启用" prop="state">
          <el-input v-model="form.state" placeholder="请输入是否启用(1：启用，2：禁用）" />
        </el-form-item> -->
        <el-form-item label="选择类别" prop="state">
          <el-select v-model="form.state" placeholder="请选择" value-key="value">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传" prop="filePath" v-if="form.state">
          <ImageUpload :value="form.filePath" v-if="form.state=='1'" @input="handleFile"></ImageUpload>
          <ImageUpload :value="form.filePath" v-if="form.state=='2'" @input="handleFile"></ImageUpload>
          <FileUpload :value="form.filePath" v-if="form.state=='3'" @input="handleFile"></FileUpload>
          <!-- <ImageUpload :value="form.filePath" v-if="form.state=='3'" @input="handleFile"></ImageUpload> -->
          <!-- <ImageUpload :value="form.filePath" v-if="form.state=='1'" @input="handleFile"></ImageUpload> -->
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCrop, getCrop, delCrop, addCrop, updateCrop } from "@/api/system/crop";
import { listImageCrop } from "@/api/system/ImageCrop";
export default {
  name: "Crop",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 作物信息表格数据
      cropList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        collectTime: null,
        collectAddr: null,
        cropType: null,
        cropPp: null,
        cropGg: null,
        cropPd: null,
        sfqfyy: null,
        state: null,
      },
      // ,{label:'单株高光谱图像',value:1},{label:'多株高光谱图像',value:3}
      options:[{label:'单株图像',value:1},{label:'多株图像',value:2},{label:'作物视频',value:3}],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      filePath:''
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //上传回调
    handleFile(file){
      this.filePath = file
    },
    /** 查询作物信息列表 */
    getList() {
      this.loading = true;
      listCrop(this.queryParams).then(response => {
        this.cropList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cropId: null,
        cropHeight:null,
        collectTime: null,
        collectAddr: null,
        cropType: null,
        cropPp: null,
        cropGg: null,
        cropPd: null,
        sfqfyy: null,
        state: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        filePath: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cropId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加作物信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cropId = row.cropId || this.ids
      let imgUrl = []
      getCrop(cropId).then(response => {
        this.form = response.data;
        listImageCrop({cropId:cropId}).then(response => {
          if(response.rows.length>0){
            response.rows.forEach(item => {
              imgUrl.push(item.filePath)
            })
            this.form.filePath = imgUrl.join(',')
            console.log('图片路径'+this.form.filePath)
          }
        });
        this.open = true;
        this.title = "修改作物信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cropId != null) {
            this.form.filePath=this.filePath
            updateCrop(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.filePath=this.filePath
            addCrop(this.form).then(response => {
              console.log(this.form)
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cropIds = row.cropId || this.ids;
      this.$modal.confirm('是否确认删除作物信息编号为"' + cropIds + '"的数据项？').then(function() {
        return delCrop(cropIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/crop/export', {
        ...this.queryParams
      }, `crop_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
